package com.selfdriving.activity;

import java.util.ArrayList;
import java.util.List;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

import com.selfdriving.BaseApplication;
import com.selfdriving.R;
import com.selfdriving.adapter.TravelTogetherAdapter;
import com.selfdriving.bean.Entity;
import com.selfdriving.bean.TogetherPlay;
import com.selfdriving.bean.TogetherPlayExamineBean;
import com.selfdriving.dialog.ChooseDialog;
import com.selfdriving.dialog.ChooseDialog.OnTogetherDialogClickListener;
import com.selfdriving.utils.Constant;
import com.selfdriving.utils.Utils;
import com.selfdriving.view.MoMoRefreshListView;
import com.selfdriving.view.MoMoRefreshListView.OnCancelListener;
import com.selfdriving.view.MoMoRefreshListView.OnRefreshListener;

public class MyNearbyFriend implements OnRefreshListener, OnCancelListener
{
	/** 当前View **/
	View view;
	BaseApplication application;
	Context context;
	public MoMoRefreshListView friend_list;
	// private NearbyFriendAdapter adapter;
	TravelTogetherAdapter adapter;
	public List<Entity> togetherList = new ArrayList<Entity>();
	private ChooseDialog chooseDialog;
	LayoutInflater inflater;
	private Button seach;
	LinearLayout refreshLayouTop;
	public int page = 1;

	public MyNearbyFriend(BaseApplication application, Context context,

	Button seach)
	{
		inflater = LayoutInflater.from(context);
		view = inflater.inflate(R.layout.refresh_listview_have_word, null);
		this.seach = seach;
		this.application = application;
		this.context = context;
		initViews();
		getTogetherList();
	}

	public void getTogetherList()
	{
		togetherList.clear();
		friend_list.mState = 2;
		friend_list.changeHeaderViewByState();
		AjaxParams params = new AjaxParams();
		params.put("f", "49");
		params.put("phone",
				application.user != null ? application.user.getPhone() : "");
		FinalHttp fh = new FinalHttp();
		fh.configTimeout(10000);
		fh.post(Constant.POST, params, new AjaxCallBack<Object>()
		{
			@Override
			public void onFailure(Throwable t, String strMsg)
			{
				friend_list.onRefreshComplete();
			}

			@Override
			public void onSuccess(Object o)
			{
				String json = o.toString().trim();
				try
				{
					JSONArray array = new JSONArray(json);
					TogetherPlay play;
					for (int i = 0; i < array.length(); i++)
					{
						JSONObject object = array.getJSONObject(i);
						String id = object.getString("id");
						String destination = object.getString("destination");
						String peoples = object.getString("peoples");
						String headImg = object.getString("headImg");
						String name = object.getString("name");
						String phone = object.getString("phone");
						String goType = object.getString("goType");
						String days = object.getString("days");
						String sex = object.getString("sex");
						play = new TogetherPlay(destination, id, peoples,
								headImg, name, phone, goType, days, sex);
						togetherList.add(play);
					}
					page++;
					adapter.notifyDataSetChanged();
					friend_list.onRefreshComplete();
				} catch (JSONException e)
				{
					friend_list.onRefreshComplete();
					e.printStackTrace();
				}
			}
		});
	}

	public void getTogetherExamineList(String id)
	{
		application.togetherList.clear();
		Utils.showLoadingDialog(context, "数据加载中...");
		AjaxParams params = new AjaxParams();
		params.put("f", "45");
		params.put("id", id);
		FinalHttp fh = new FinalHttp();
		fh.configTimeout(10000);
		fh.post(Constant.POST, params, new AjaxCallBack<Object>()
		{
			@Override
			public void onFailure(Throwable t, String strMsg)
			{
				Utils.showShortToast(context, "获取数据失败");
				Utils.dismissLoadingDialog();
			}

			@Override
			public void onSuccess(Object o)
			{
				String json = o.toString().trim();
				System.out.println(json);
				try
				{
					TogetherPlayExamineBean play;
					JSONObject object = new JSONObject(json);
					String id = object.getString("id");
					String headImg = object.getString("headImg");
					String route = object.getString("route");
					String fee = object.getString("fee");
					String from = object.getString("from");
					String times = object.getString("times");
					String declaration = object.getString("declaration");
					String originate = object.getString("originate");
					String sex = object.getString("sex");
					String destination = object.getString("destination");
					String phone = object.getString("phone");
					String peoples = object.getString("peoples");
					String status = object.getString("status");
					String goDate = object.getString("goDate");
					String goType = object.getString("goType");
					String days = object.getString("days");
					String car = object.getString("car");
					play = new TogetherPlayExamineBean(id, route, fee, from,
							destination, times, declaration, originate,
							headImg, phone, peoples, status, goDate, goType,
							days, sex, car);
					application.togetherList.add(play);
					adapter.notifyDataSetChanged();
					Utils.dismissLoadingDialog();
					Intent intent = new Intent(context,
							TogetherPlayExamine.class);
					context.startActivity(intent);
				} catch (JSONException e)
				{
					Utils.showShortToast(context, "获取数据失败");
					Utils.dismissLoadingDialog();
					e.printStackTrace();
				}
			}
		});
	}

	public void initViews()
	{
		refreshLayouTop = (LinearLayout) view.findViewById(R.id.refresh_top);
		refreshLayouTop.setVisibility(View.GONE);
		friend_list = (MoMoRefreshListView) view
				.findViewById(R.id.refresh_list);
		chooseDialog = new ChooseDialog(application, context, "结伴游");
		adapter = new TravelTogetherAdapter(application, context, togetherList);
		friend_list.setOnCancelListener(this);
		friend_list.setOnRefreshListener(this);
		friend_list.setAdapter(adapter);
		// seach.setOnClickListener(new OnClickListener()
		// {
		// @Override
		// public void onClick(View v)
		// {
		// chooseDialog.show();
		// }
		// });

		friend_list.setOnItemClickListener(new OnItemClickListener()
		{

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3)
			{
				if (arg2 == 0)
				{
					return;
				}
				TogetherPlay play = (TogetherPlay) togetherList.get(arg2 - 1);
				getTogetherExamineList(play.getId());
				// Intent intent = new Intent(context,
				// TravelDetailsActivity.class);
				// context.startActivity(intent);
			}
		});
		chooseDialog
				.setOnTogetherDialogClickListener(new OnTogetherDialogClickListener()
				{

					@Override
					public void OnOkClick(String sex, String time, String age,
							String car, String togetherWayId)
					{
						// TODO Auto-generated method stub
						System.out.println("MyNearbyFriend");
						System.out.println(sex);
						System.out.println(time);
						System.out.println(age);
						System.out.println(car);
						System.out.println(togetherWayId);
						chooseDialog.dismiss();

					}

					@Override
					public void OnCancelClick()
					{
						chooseDialog.dismiss();
					}
				});
	}

	public View getView()
	{
		return view;
	}

	@Override
	public void onCancel()
	{
		// TODO Auto-generated method stub
		friend_list.onRefreshComplete();
		friend_list.onRefreshComplete();
	}

	@Override
	public void onRefresh()
	{
		// TODO Auto-generated method stub
		page = 1;
		getTogetherList();
	}

	@Override
	public void onLoadMore()
	{
		// TODO Auto-generated method stub
		getTogetherList();
		friend_list.onRefreshComplete();
	}

}
